package org.pluslibs.framework.swingplus.utils;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JToggleButton;
        
public class MediaCache {
    
    /* *********************************************** */
    // Constants
    
    private static ImageIcon ICON_ASCENDING  = null;
    private static ImageIcon ICON_DESCENDING = null;
    private static ImageIcon ICON_ERRORICON  = null;
    private static ImageIcon ICON_APPLICATIONLOGO = null;
    
    public static ImageIcon getIconAscending() {
        if ( ICON_ASCENDING==null ) {
        	ICON_ASCENDING = readImageIcon("/media/ascending.png");
        }
        return ICON_ASCENDING;
    }
    public static ImageIcon getIconDescending() {
        if ( ICON_DESCENDING==null ) {
        	ICON_DESCENDING = readImageIcon("/media/descending.png");
        }
        return ICON_DESCENDING;
    }
    public static ImageIcon getErrorIcon() {
        if ( ICON_ERRORICON==null ) {
        	ICON_ERRORICON = readImageIcon("/media/erroricon.png");
        }
        return ICON_ERRORICON;
    }
    public static ImageIcon getApplicationLogo() {
        if ( ICON_APPLICATIONLOGO==null ) {
        	ICON_APPLICATIONLOGO = readImageIcon("/media/applicationLogo.png");
        }
        return ICON_APPLICATIONLOGO;
    }
    
    public static JLabel getApplicationLogoLabel() {
    	ImageIcon icon = getApplicationLogo();
    	Dimension size = new Dimension(icon.getIconWidth(), icon.getIconHeight());
    	JLabel label = new JLabel(icon);
    	label.setMinimumSize(size);
    	label.setPreferredSize(size);
    	label.setMaximumSize(size);
    	return label;
    }
    
    /* *********************************************** */
    // Create Styled Buttons
    
    private static final Insets NO_MARGIN = new Insets(0,0,0,0);
    
    public static JButton createButtonForward1(String tooltip, ActionListener listener) {
    	return createButton(tooltip, "blueForward1", listener);
    }
    public static JButton createButtonForward2(String tooltip, ActionListener listener) {
    	return createButton(tooltip, "blueForward2", listener);
    }
    public static JButton createButtonHome(String tooltip, ActionListener listener) {
    	return createButton(tooltip, "blueHome", listener);
    }
    private static JButton createButton(String tooltip, String name, ActionListener listener) {
    	JButton button = new JButton(readImageIcon("/media/" + name + "-Up.png"));
    	initIconButton(button, tooltip, listener);
    	button.setPressedIcon(readImageIcon("/media/" + name + "-Down.png"));
    	return button;
    }
    
    public static JToggleButton createToogleRecordButton(String tooltip, ActionListener listener) {
    	JToggleButton button = new JToggleButton(readImageIcon("/media/blueRecordingEnabled-Up.png"));
    	initIconButton(button, tooltip, listener);
    	button.setSelectedIcon(readImageIcon("/media/blueRecordingDisabled-Down.png"));
    	return button;
    }
    public static JToggleButton createTooglePlayPauseButton(String tooltip, ActionListener listener) {
    	JToggleButton button = new JToggleButton(readImageIcon("/media/bluePlay-Up.png"));
    	initIconButton(button, tooltip, listener);
    	button.setSelectedIcon(readImageIcon("/media/bluePaused-Down.png"));
    	return button;
    }
    
    private static void initIconButton(AbstractButton button, String tooltip, ActionListener listener) {
    	if ( tooltip!=null ) button.setToolTipText(tooltip);
    	if ( listener!=null ) button.addActionListener(listener);
    	button.setBorderPainted(false);
    	button.setContentAreaFilled(false);
    	button.setMargin(NO_MARGIN);
    }
    
    /* *********************************************** */
    // Helpers
    
    private static ImageIcon readImageIcon(String filename) {
        URL url = MediaCache.class.getResource(filename);
        if ( url==null ) {
            throw new RuntimeException("Icon \"" + filename + "\" has not been copied to media");
        } else {
            return new ImageIcon(url);
        }
    }
    
    /*private static BufferedImage readBufferedImage(String filename) throws IOException {
    	filename = BASEDIR + "/" + filename;
        InputStream stream = MediaCache.class.getResourceAsStream(filename);
        if ( stream==null ) {
            throw new RuntimeException("Image \"" + filename + "\" has not been copied to media");
        } else {
            return ImageIO.read(stream);
        }
    }*/

    /* *********************************************** */
}
